const path = require("path");
//const projectRoot = path.resolve(__dirname, "../");
const webpack = require("webpack");
module.exports = {
  pages: {
    index: {
      // page 的入口
      entry: "src/main.js",
      // 模板来源
      template: "public/index.html",
      // 在 dist/index.html 的输出
      filename: "index.html",
      // 当使用 title 选项时，
      // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
      title: "Index Page",
      // 在这个页面中包含的块，默认情况下会包含
      // 提取出来的通用 chunk 和 vendor chunk。
      chunks: ["chunk-vendors", "chunk-common", "index"]
    },
    // 当使用只有入口的字符串格式时，
    // 模板会被推导为 `public/subpage.html`
    // 并且如果找不到的话，就回退到 `public/index.html`。
    // 输出文件名会被推导为 `subpage.html`。
    signin: {
      entry: "src/subpage/main.js",
      template: "public/sign.html",
      filename: "sign.html"
    }
  },
  devServer: {
    historyApiFallback: {
      rewrites: [
        { from: /\/index/, to: "/index.html" },
        { from: /\/sign/, to: "/sign.html" }
      ]
    }
  },
  pluginOptions: {
    "serve-api-mocks": {
      base: "/api",
      routes: [
        {
          method: "GET",
          callback(req, res) {
            res
              .status(200)
              .send("OK")
              .end();
          }
        },
        {
          path: "/redirect_uri",
          relative: false,
          callback(req, res) {
            res
              .status(200)
              .send("OK")
              .end();
          }
        }
      ]
    }
  },
  configureWebpack: {
    // 移除 prefetch 插件 提高带宽
    // config.plugins.delete('prefetch')
    plugins: [
      new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery"
      })
    ],
    devServer: {
      historyApiFallback: true
    },
    externals: {
      // global app config object
      config: JSON.stringify({
        apiUrl: "http://localhost"
      })
    },
    module: {
      rules: [
        { test: /\.xml$/, loader: "xml-loader" } // will load all .xml files with xml-loader by default
      ]
    },
    resolve: {
      extensions: [".js", ".vue", ".css", ".json"],
      alias: {
        package: path.resolve(__dirname, "./package.json"),
        semantic: path.resolve(__dirname, "./semantic"),
        mixins: path.resolve(__dirname, "./src/mixins"),
        services: path.resolve(__dirname, "./src/services"),
        assets: path.resolve(__dirname, "./src/assets"),
        views: path.resolve(__dirname, "./src/views")
        // 'semantic-calendar': path.resolve(__dirname, './node_modules/semantic-ui-calendar/dist/calendar.js'),
        // 'semantic-calendar-css': path.resolve(__dirname, './node_modules/semantic-ui-calendar/dist/calendar.css')
      }
    }
  }
};
